﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Arizu.Repository;
using System.Data;

namespace AiRiZu.User
{
    public partial class Confirm : Common
    {
        //public string TeSeimgurl = "images/default_user_pic.png";

        //public string OwnerImgurl = "images/default_user_pic.png";

        

        //public string RoomOwner_Name = "";

        //public string RoomOwner_Adress = "";

        //public string RoomOwner_School = "";

        //public string RoomOwner_Work = "";

        //public string You_Adress = "";

        //public string You_School = "";

        public string You_Work = "";


        public string RoomName = "";

        public string RoomAddr = "";

        public string CheckInDate = "";

        public string CheckOutDate = "";

        public int NumOfPeople = 0;

        public int NumOfNight = 0;

        public int SubtotalMoney = 0;

        public int TotalMoney = 0;

        public int FrontMoney = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            AirizuUser userDao = AirizuUser.CreateInstance();
            int userId = -1;

            CheckInDate = Session["checkIn"] == null ? "" : Session["checkIn"].ToString();
            CheckOutDate = Session["checkOut"] == null ? "" : Session["checkOut"].ToString();
            NumOfPeople = int.Parse(Session["ruzhupeoplenum"] == null ? "0" : Session["ruzhupeoplenum"].ToString());

            DateTime checkinDateTime = DateTime.ParseExact(CheckInDate, "yyyy-MM-dd", null);
            DateTime checkoutDateTime = DateTime.ParseExact(CheckOutDate, "yyyy-MM-dd", null);

            TimeSpan checkinTimeSpan = new TimeSpan(checkinDateTime.Ticks);
            TimeSpan checkoutTimeSpan = new TimeSpan(checkoutDateTime.Ticks);

            TimeSpan span = checkoutTimeSpan.Subtract(checkinTimeSpan);
            NumOfNight = Convert.ToInt32(span.TotalDays) + 1;

            if (Session["logId"] != null)
            {
                userId = int.Parse(Session["logId"].ToString());
                DataTable dt = userDao.GetUser(userId);

                if (dt.Rows.Count > 0)
                {
                    You_Work = dt.Rows[0]["Work"].ToString();
                }
            }

            if (Session["roomId"] != null)
            {
                AirizuRoom airizuRoom = AirizuRoom.CreateInstance();
                DataTable dt1 = airizuRoom.GetRoom(long.Parse(Session["roomId"].ToString()));
                if (dt1.Rows.Count > 0)
                {
                    long ownerId = long.Parse(dt1.Rows[0]["OwnerID"].ToString());
                    RoomName = dt1.Rows[0]["Name"].ToString();
                    RoomAddr = dt1.Rows[0]["Province"].ToString() + dt1.Rows[0]["City"].ToString() + dt1.Rows[0]["Area"].ToString() + dt1.Rows[0]["Adressdesc"].ToString();

                    AirizuPrice airizuPrice = AirizuPrice.CreateInstance();
                    DataTable dt = airizuPrice.GetPrice(long.Parse(Session["roomId"].ToString()));

                    if (NumOfNight > 30 && dt.Rows[0]["MonthPrice"].ToString() != null)
                    {
                        GetPrice(int.Parse(dt.Rows[0]["MonthPrice"].ToString()), NumOfNight);
                    }
                    else if ((NumOfNight < 30 && NumOfNight > 7) && dt.Rows[0]["WeekPrice"].ToString() != null)
                    {
                        GetPrice(int.Parse(dt.Rows[0]["WeekPrice"].ToString()), NumOfNight);
                    }
                    else if (NumOfNight < 7 && dt.Rows[0]["DayPrice"].ToString() != null)
                    {
                        GetPrice(int.Parse(dt.Rows[0]["DayPrice"].ToString()), NumOfNight);
                    }
                }
            }   
        }

        private void GetPrice(int price, int num)
        {
            SubtotalMoney = price * num;

            TotalMoney = price * num;

            FrontMoney = price * num;
        }
    }
}
